#!/usr/bin/perl
#vgdelete.cgi
require './lvm-lib.pl';
&ReadParse();
my $lvname = $in{'lv'};
my ($err_msg, $l, $lv, @lvinfo);
@lvinfo = get_lv_info();
if (!$lvname) {
	$err_msg = $text{'err_noselectlv'};
	goto ERROR_OUT;
}
my $found = 0;
foreach $l (@lvinfo) {
	if ($l->{'lv'} eq $lvname) {
		$found = 1;
		$lv = $l;
		last;
	}
}

if (!$found) {
	$err_msg = $text{'err_lvnofound'};
	goto ERROR_OUT;
}
if($lv->{'snapo'}){
	$err_msg = $text{'err_lv_exist_snapshot'};
	goto ERROR_OUT;
}
$err_msg = $lv->{'lv'}.$lv->{'lvname'}.$lv->{'lvpath'}.$lv->{'status_m'};
#goto ERROR_OUT;
if($lv->{'status_m'}){
	`umount $lv->{'lvpath'}`;
	if($?){
		$err_msg = $text{'err_lvbusy'};
		goto ERROR_OUT;
	}
}
if($lv->{'status_i'}){
	$err_msg = $text{'err_iscsi_lv_nodelete'};
	goto ERROR_OUT;
}

#cryptsetup remove lvname firstly
if(-e "/dev/mapper/$lv->{'lvname'}") {
	`cryptsetup remove $lv->{'lvname'}`;
}

#now remove lv
my $rc = remove_lv($lv);
if(!$rc){
	delete_voltab();
	&redirect("index.cgi");
}
elsif($rc eq 1280){
	$err_msg = $text{'err_lvdelete'}; #iscsi?
	goto ERROR_OUT;
}
else{
	$err_msg = $text{'err_lvdelete'};
	goto ERROR_OUT;
}
return;
ERROR_OUT:
&error_setup($text{'crt_err_title'});
&error($err_msg);
